﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Nancy;
using Nancy.Security;
using Nancy.ModelBinding;
using PlatWL.Data;
using PlatWL.Services.ServicesWeb;

namespace PlatWL.Modules.Web
{
    public class AuthModule : MsgModule
    {
        public AuthModule(AuthManage service) : base("auth")
        {
            //发送验证码
            Get["/sendsms/{telephone}/{type?''}"] = p =>
            {
                service.SendSms(ref msg, Client, p.telephone, p.type);
                return Response.AsJson(msg);
            };
            //获取随机码，返回图片
            Get["/rancode/{telephone}/{type?''}"] = p =>
            {
                Stream img = service.RandomCode(ref msg, Client, p.telephone, p.type);
                return Response.FromStream(img, "image/png");
            };
            //登录
            Post["/login"] = p =>
            {
                string telephone = this.Request.Form["Telephone"];
                string password = this.Request.Form["Password"];
                if (string.IsNullOrEmpty(telephone) || string.IsNullOrEmpty(password))
                {
                    msg.error = "请输入手机号和密码";
                }
                else
                {
                    service.Login(ref msg, Client, telephone, password);
                }

                return Response.AsJson(msg);
            };

            //注册
            Post["/reg"] = p =>
            {
                string telephone = this.Request.Form["Telephone"];
                string password = this.Request.Form["Password"];
                string smscode = this.Request.Form["smscode"]; //随机码
                string rdm = this.Request.Form["rdm"]; //随机标识码
                string refid = this.Request.Form["ref"] ?? "";
                string code = this.Request.Form["code"] ?? "";

                if (string.IsNullOrEmpty(telephone) || string.IsNullOrEmpty(password)||string.IsNullOrEmpty(smscode))
                {
                    msg.error = "请输入手机号,密码和验证码";
                }
                else
                {
                    service.Reg(ref msg, Client, telephone, password, smscode, refid, rdm, code);
                }

                return Response.AsJson(msg);
            };
            Get["/wxcode/{code}"] = p =>
            {
                service.WXCode(ref msg, Client, p.code);
                return Response.AsJson(msg);
            };
            Get["/tui/{id:int}"] = p =>
            {
                service.GetTui(ref msg, Client, p.id);
                return Response.AsJson(msg);
            };
            


        }
    }
}